package service

import (
	"context"
	"strconv"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
)

// CommissionedClassificationService acts as a struct for injecting an implementation of EmployeeRepository
// for use in service methods
type CommissionedClassificationService struct {
	CommissionedClassificationRepository model.CommissionedClassificationRepository
	PaymentClassificationRepository      model.PaymentClassificationRepository
}

// CCConfig will hold repository that will eventually be injected into this
// service layer
type CCConfig struct {
	CommissionedClassificationRepository model.CommissionedClassificationRepository
	PaymentClassificationRepository      model.PaymentClassificationRepository
}

// NewCommissionedClassificationService is a factory function for
// initializing a EmployeeService with its repository layer dependencies
func NewCommissionedClassificationService(c *CCConfig) model.CommissionedClassificationService {
	return &CommissionedClassificationService{
		CommissionedClassificationRepository: c.CommissionedClassificationRepository,
		PaymentClassificationRepository:      c.PaymentClassificationRepository,
	}
}

func (c *CommissionedClassificationService) Update(ctx context.Context, id int64, cc *model.CommissionedClassification) error {
	paymentClassification, err := c.PaymentClassificationRepository.FindByEmlpoyeeID(ctx, id)
	if err != nil {
		return apperrors.NewNotFound("paymentClassificationID", strconv.Itoa(int(id)))
	}

	cc.PaymentClassificationID = paymentClassification.ID

	err = c.CommissionedClassificationRepository.Update(ctx, cc)

	return err
}
